import TableLyout from "@/components/commonPublic/tableLyout.vue";
import publicForm from "@/components/materialDataManage/fromCom.vue"; //模糊查询
import TableData from "@/components/public/el-table"; //主页面列表
import CustomDialog from "@/components/sampleManage/fieldSampling/index.vue";
import CustomDialog1 from "@/components/testDetectionManage/testingLedger/index.vue";
import formJs from "./regexDetail.js"; //新增弹框配置项
import actions from "@/actions";

import {
    getSampleTestWorkJLList, //主页面列表接口
    synchronousTest, //同步
    revokeSampleTest,//撤销
} from "@/api/request.js";
// import { statusType2 } from "@/utils/config"; //配置项
export default {
    name: "testingLedger",
    components: {
        TableLyout,
        publicForm, //模糊查询
        TableData, //主页面列表
        CustomDialog, //弹框组件
        CustomDialog1,//撤回状态
    },
    data() {
        return {
            name: "testingLedger", //按钮配置页面代号
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            userNature: null, //用户Nature

            projectsObj: [], //项目名
            tendersObj: [],
            falgId: null,
            // 列表
            btnObj: formJs.btnObj, //按钮
            table_config: formJs.table_config,
            tableData: [], //列表数据列表
            // checked: true, //查看下级按钮
            loading: false, //列表数据加载loading
            getList: {
                fprojectid: "",
                fCompanyid: "", // 左侧选中组织机构id
                startDates: "", // 开始时间
                endDates: "", // 截至时间
                keyWord: "", // 上方查询条件样品名称
                page: 1, // 当前页码
                pageSize: 15, // 每页显示条数
                userId: "",
                keyType: "", // 样品类型
                modelType: "0",
                timeDates: "",
                fSamplenostr:""
            }, // 列表给后端传参
            // 列表
            dataFid: null,
            // 弹框组件
            currentRow: null, //列表详情
            isShow: false, //是否显示弹框
            statusNum: null, //点击那个按钮
            displaySwitching: 2, //2取样&收样；0取样；1收样
            dialogTitle: null,
            isShow1: false, //状态撤销弹框
            // 弹框组件

            // 左侧机构树
            treelength: false, //左侧树显示隐藏
            showName: "未选择公司",
            treeData: [], //左侧树状图数据
            defaultProps: {
                children: "children",
                label: "fShortname",
            },
            checkData: [],
            treeText: {
                companyId: null,
                text: null,
                userId: null,
            },
            // 左侧机构树

            userInfo: {}, //登录用户
        };
    },

    mounted() {
        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser
        }, true);
        this.getDate();

        this.table_config.operation.cols = [{
            prop: "fReportstatus",
            type: "primary",
            handleRow: this.handleRow,
        },];

        this.table_config.operation["specialClass"] = function (rowData) {
            var color = null;
            if (rowData == "待编制") {
                color = "red !important";
            } else if (rowData == "已完成") {
                color = "green !important";
            } else {
                color = "#409eff !important";
            }
            return color;
        };
    },
    methods: {
        reset(e) {
            this.getList = this.$options.data.call(this).getList
            this.getList.fprojectid = e
        },
        // 选择项目名称
        selectCompany(e) {
            this.companyName = e.F_ProjectName;
            this.getList.fDepartmentid = e.F_ProjectId;
            this.projectName = "";
            this.getList.fCompanyid = "";
            // 调用施工单位数据
            this.getCompanyIdAndName(e.F_ProjectId);

            this.yj_searchForm(this.getList);
        },
        // 选择施工单位
        selectProject(e) {

            this.projectName = e.F_CompanyName;
            this.getList.fCompanyid = e.F_CompanyId;
            // this.yj_searchForm(this.getList);
        },
        async getTender(e) {
            await this.$http(
                " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
                e, {
                headers: {
                    "Content-Type": "application/json",
                    Authorization: localStorage.getItem("access_token"),
                },
            }
            ).then((res) => {
                if (res.status == 200) {
                    var data = res.data.data.GXCompanyList;
                    this.tendersObj = [];
                    if (data.length == 0) {
                        this.$message.warning({
                            content: "有其他人员登录此账号！",
                            duration: 1000,
                        });
                        setTimeout(() => {
                            window.location.href = "http://172.18.5.201:1080/login";
                        }, 1200);
                    } else  if (data.length == 1) {
                        data.map((item) => {
                            this.tendersObj.push({
                                fItemName: item.sectionName,
                                fItemValue: item.sectionId,
                            });
                        });
                        this.getList.fCompanyid = data[0].sectionId;
                    } else {
                        this.tendersObj = [{
                            fItemName: "所有标段",
                            fItemValue: "",
                        },];
                        data.map((item) => {
                            this.tendersObj.push({
                                fItemName: item.sectionName,
                                fItemValue: item.sectionId,
                            });
                        });
                    }
                }
            });
        },
        selectTender(e) {
            this.getList.stationId = "";
            this.getList.machineId = "";
            this.bhzDataList = [];
            this.bhjDataList = [];
        },
        getBtnName(name, status) {
            this.statusNum = status
            this.yj_edit(status)
        },

        // 新增/编辑
        yj_edit(num) {
            this.statusNum = num
            // this.dialogTitle = "闭合处理"
            if (num == 1) { // 导出
                if (this.dataFid != null) {
                    // this.editText();
                } else {
                    this.$message.warning({
                        content: "请先选择需要导出的数据",
                        duration: 1000
                    });
                }
            } else if (num == 2) { //同步
                this.synchronous();
            } else if (num == 3) { //撤销
                if (this.dataFid != null) {
                    this.revoke(this.dataFid);
                } else {
                    this.$message.warning({
                        content: "请先选择需要撤销的数据",
                        duration: 1000
                    });
                }
            } else if (num == 4) { //退回
                if (this.dataFid != null) {
                    this.isShow1 = true;
                } else {
                    this.$message.warning({
                        content: "请先选择需要退回的数据",
                        duration: 1000
                    });
                }
            } else if (num == 7) { //查看
                if (this.dataFid != null) {
                    this.$router.push({
                        path: "/testDetectionManage/samplesLedgerText",

                    });
                    var data = {
                        data: this.currentRow,
                        getList: this.getList,
                        status: 7,
                        displaySwitching: 2,
                        changeBtn: true,
                    }
                    sessionStorage.setItem(
                        "GxSampleManage_fieldSampling",
                        JSON.stringify(data)
                    );
                } else {
                    this.$message.warning({
                        content: "请先选择需要查看的数据",
                        duration: 1000
                    });

                }
            }

        },
        // 同步
        synchronous() {
            this.loading = true;
            synchronousTest().then(res => {
                if (res.errcode == 0) {
                    this.yj_searchForm(this.getList);
                    this.loading = false;
                    return false;
                } else {
                    this.loading = false;
                }

            })
        },
        // 撤销
        revoke(id) {
            var data = {
                fId: id,
                userId: this.userInfo.id, // 当前登录人id
                userName: this.userInfo.userName, // 当前登录人姓名
            }
            this.loading = true;
            revokeSampleTest(data).then(res => {
                if (res.errcode == 0) {
                    this.$message.success(res.msg);
                    this.yj_searchForm(this.getList);
                    this.loading = false;
                    return false;
                } else {
                    this.$message.warning({
                        content: "撤销:"+res.msg,
                        duration: 1000
                    });
                    this.loading = false;
                }
            })
        },


        // 获取列表数据
        yj_searchForm(data) {
            window.sessionStorage.setItem(
                "GxEmbedded-testDetectionManage_getList",
                JSON.stringify(data)
            );
            var id;
            if (data.fCompanyid != "") {
                id = data.fCompanyid;
            } else {
                id = data.fprojectid;
            }
            var obj = {
                fCompanyid: id, //左侧选中组织机构id
                startDates: data.startDates, //进场开始时间
                endDates: data.endDates, //进场结束时间
                keyWord: data.keyWord, // 上方查询条件样品名称
                page: data.page, //当前页码
                pageSize: data.pageSize, //每页显示条数
                userId: this.userInfo.id, //查询待办或已办时传 列表页查询不传
                keyType: data.keyType, //上方查询条件材料种类
                modelType: "0",
                fSamplenostr:data.fSamplenostr

                // endDates: "2024-03-01 23:59:59",
                // fCompanyid: "51c92c76-aa35-47ce-a8d8-0b6473b72fbe",
                // keyType: "",
                // keyWord: "",
                // modelType: "0",
                // page: data.page, //当前页码
                // pageSize: data.pageSize, //每页显示条数
                // startDates: "2023-03-02 00:00:00",
                // userId: "System",
            };
            this.loading = true;
            var that = this;
            getSampleTestWorkJLList(obj)
                .then((res) => {
                    console.log("列表", res);
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        this.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error({
                            content: "列表数据获取失败" + res.msg,
                            duration: 1000
                        });
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    console.log("列表数据 接口出错" + error.msg);
                    this.loading = false;
                });
        },

        // 查看
        yj_viewForm(row, label) {
            if (label == "样品编号") {
                if(row.fCompanyid==""){
                    this.$message.warning({
                        content: "暂无样品信息",
                        duration: 1000
                    });
                }else {
                this.currentRow = row;
                this.yj_edit(7)
                }
            }
            if (label == "报告编号") {
                if (row.fReporturl != "" && row.fReporturl != "/" && row.fReporturl != null) {
                    window.open(row.fReporturl, '_blank')

                } else {
                    this.$message.warning({
                        content: "试验报告暂未出具",
                        duration: 1000
                    });
                }

            }
        },
        // 点击当前行
        rowChange(val) {
            this.currentRow = val;
            this.dataFid = val.fId;
            return val;
        },

        //获取时间
        getDate() {
            if (JSON.parse(sessionStorage.getItem("GxEmbedded-testDetectionManage_getList"))) {

                var dataList = JSON.parse(
                    sessionStorage.getItem("GxEmbedded-testDetectionManage_getList")
                );
                if (dataList.fprojectid != "") {
                    this.getTender(dataList.fprojectid);
                } else {
                    data.fprojectid = this.projectsObj[1].fItemValue;
                    this.getTender(dataList.fprojectid);
                }
                if (dataList.startDates == "" || dataList.startDates == undefined || dataList.startDates == null || dataList.endDates == "" || dataList.endDates == undefined || dataList.endDates == null) {
                    dataList.startDates = this.common.getDay(-90); // 开始时间
                    dataList.endDates = this.common.getDay(0); // 截至时间
                }
                this.getList = {
                    fprojectid: dataList.fprojectid,
                    fCompanyid: dataList.fCompanyid, // 左侧选中组织机构id
                    page: 1, // 当前页码
                    pageSize: 15, // 每页显示条数
                    userId: this.userInfo.id,
                    modelType: "1",
                    keyWord: "", // 上方查询条件样品名称

                    keyType: "", // 样品类型
                    startDates: dataList.startDates, // 开始时间
                    endDates: dataList.endDates, // 截至时间
                    timeDates: [dataList.startDates, dataList.endDates],
                    fSamplenostr:dataList.fSamplenostr
                };
            } else {
                this.getList.startDates = this.common.getDay(-90); // 开始时间
                this.getList.endDates = this.common.getDay(0); // 截至时间
                this.getList.timeDates = [
                    this.getList.startDates,
                    this.getList.endDates,
                ];
            }
        },

        // 弹框组件
        refreshList() {
            //刷新列表
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            });
        },
        closeDialog() {
            this.isShow = false;
            this.isShow1 = false;
        },
        // 弹框组件




        // 筛选条件

        // 时间间隔
        selectTime(e) {
            if (e[0] != "" && e[1] != "") {
                this.getList.startDates = e[0] + " " + "00:00:00"; // 开始时间
                this.getList.endDates = e[1] + " " + "23:59:59"; // 截至时间
            } else {
                this.getList.startDates = ""; // 开始时间
                this.getList.endDates = ""; // 截至时间
            }
            this.getList.timeDates = [e[0], e[1]];
            this.yj_searchForm(this.getList);
        },
        // 筛选条件
    },
};